package com.bjpowernode.app.controller;

import com.bjpowernode.app.beans.ClassRoom;
import com.bjpowernode.app.exception.MyException;
import com.bjpowernode.app.services.ClassRoomServices;
import com.bjpowernode.app.services.ClassRoomServicesImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/ClassRoomController.action")
public class ClassRoomController extends HttpServlet {

    ClassRoomServices classRoomServices = new ClassRoomServicesImp();
    HttpServletRequest req;
    HttpServletResponse resp;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.req = req;
        this.resp = resp;
        //设置中文编码utf-8
        //请求对象设置
        req.setCharacterEncoding("utf-8");
        //响应对象设置
        resp.setContentType("text/html;charset=UTF-8");

        //可以使用get传递参数的方案去选择执行某个控制器业务
        //option参数是功能的意识
        //option参数值只能是：view|add|edit|editBefore|del
        String option = req.getParameter("option");
        if (option == null) {
            req.setAttribute("mess", "请求错误，没有option参数");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            return;
        }
        if (!(option.equals("view") || option.equals("add") || option.equals("edit") || option.equals("editBefore") || option.equals("del"))) {
            req.setAttribute("mess", "option参数值只能是：view|add|edit|editBefore|del");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            return;
        }

        switch (option){
            case "view":
                getAll();
                break;
            case "add":
                add();
                break;
            case "edit":
                edit();
                break;
            case "editBefore":
                editBefore();
                break;
            case "del":
                del();
                break;
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    //查询所有
    private void getAll() throws IOException, ServletException {
        ArrayList<ClassRoom> all = classRoomServices.getAll();
        //把查询到的数据，放入到请求对象里面
        req.setAttribute("classRooms", all);
        //使用请求对象继续请求[跳转forward]
        req.getRequestDispatcher("classRoomView.jsp").forward(req, resp);
    }

    //添加
    private void add() throws ServletException, IOException {
        //前端请求带了一个参数为name，表示班级的名称，且前端请求的一切参数都是String类型
        //使用请求对象收集前端请求带来的参数
        String name = req.getParameter("name");
        //组装成一个班级对象
        ClassRoom classRoom = new ClassRoom();
        classRoom.setName(name);
        try {
            classRoomServices.add(classRoom);
        } catch (MyException e) {
            //添加错误
            req.setAttribute("mess", e.getMessage());
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        //添加正确
        //如果是添加，那么采用跳转的方法forward，则会引发重复提交问题，因为地址没变，浏览器刷新，则再次提交当前地址
        //req.getRequestDispatcher("/ClassRoomViewController.action").forward(req,resp);
        //后端Java除了跳转的方法forward，还有一种重定向方法【属于get提交，相当于在浏览器地址栏输入一个网址，然后按回车】
        //重定向使用的不是请求对象，而使用的是响应对象
        resp.sendRedirect("ClassRoomController.action?option=view");
    }

    //修改之前
    private void editBefore() throws ServletException, IOException {


        String id = req.getParameter("id");
        if (id == null) {
            req.setAttribute("mess", "必须输入修改的ID参数");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }


        int idTemp = 0;
        try {
            idTemp = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            req.setAttribute("mess", "修改的ID必须是数字");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }



        ClassRoom classRoom = classRoomServices.get(idTemp);
        if(classRoom==null){
            req.setAttribute("mess", "没有查询到要修改的班级ID"+idTemp);
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        //准备跳转
        req.setAttribute("classRoom", classRoom);
        //安排一个标记
        req.setAttribute("tag", 1);
        req.getRequestDispatcher("classRoomSave.jsp").forward(req, resp);
    }
    //修改
    private void edit() throws ServletException, IOException {

        String id = req.getParameter("id");
        if (id == null) {
            req.setAttribute("mess", "必须输入修改的ID参数");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }

        int idTemp = 0;
        try {
            idTemp = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            req.setAttribute("mess", "修改的ID必须是数字");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }

        String name = req.getParameter("name");
        if (name == null) {
            req.setAttribute("mess", "name不能为空");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }

        //组织对象
        ClassRoom classRoom=new ClassRoom();
        classRoom.setId(idTemp);
        classRoom.setName(name);
        try {
            //交给业务
            classRoomServices.edit(classRoom);
        } catch (MyException e) {
            //业务报错
            req.setAttribute("mess", e.getMessage());
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        //重定向
        resp.sendRedirect("ClassRoomController.action?option=view");
    }

    //删除
    private void del() throws IOException, ServletException {

        String id = req.getParameter("id");
        if (id == null) {
            req.setAttribute("mess", "没有给出ID无法删除");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }
        //转型问题，由控制器解决
        int idTemp = 0;
        try {
            idTemp = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            req.setAttribute("mess", "删除的ID只能是数字");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }


        try {
            classRoomServices.del(idTemp);
        } catch (MyException e) {
            req.setAttribute("mess", e.getMessage());
            req.getRequestDispatcher("error.jsp").forward(req, resp);
            return;
        }

        resp.sendRedirect("ClassRoomController.action?option=view");
    }


}
